package backs.dataBase;

import backs.Main;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DataBaseController //数据库操作类
{
	public static String ipURL="139.199.90.125:3306";
	Connection connection;
	Driver driver;
	static DataBaseController myBase=null;
	String url="jdbc:mysql://"+ipURL+"/qai?user=root&password=19951017xxr&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
	private DataBaseController()
	{
		try 
		{
			Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			connection = DriverManager.getConnection(url);
			driver = DriverManager.getDriver(connection.getMetaData().getURL());
		} catch (Exception e) 
		{
			// TODO Auto-generated catch block
			Main.addError("DBConroller",e.getMessage());
			e.printStackTrace();
		}
	}
	
	public Connection getConnection()
	{
		return connection;
	}
	
	public static DataBaseController getDBConnecterInstance()
	{
		if(myBase==null) myBase=new DataBaseController();
		try {
			if(myBase.connection.isClosed()) myBase=new DataBaseController();
			if(!myBase.connection.isValid(10))myBase=new DataBaseController();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Main.addError("DBConroller-getDBConnecterInstance",e.getMessage());
		}
		return myBase;
	}
	
	
}
